package com.xdw.nhdemo.controller;

import com.xdw.nhdemo.common.ApiConstant;
import com.xdw.nhdemo.common.ApiResult;
import com.xdw.nhdemo.dao.UserDao;
import com.xdw.nhdemo.entity.User;
import com.xdw.nhdemo.query.UserQuery;
import com.xdw.nhdemo.service.UserService;
import com.xdw.nhdemo.vo.UserVo;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Administrator
 */
@RestController
public class UserController {
    @Autowired
    UserService userService;

    @Autowired
    UserDao userDao;
    /*@PostMapping("/login")
    public boolean login(String username,String password){

        System.out.println("username="+username+",password="+password);
        if("admin".equals(username) && "123456".equals(password)){
            return true;
        }
   *//*     if(username!=null && password!=null){
            if(username.equals("admin") && "123456".equals(password)){
                return true;
            }
        }*//*

        return false;
    }*/

    @PostMapping("/login")
    public UserVo login(String username, String password){
        System.out.println("username="+username+",password="+password);
        /*UserVo user =  new UserVo();
        if("admin".equals(username) && "123456".equals(password)){
            user.setId(1);
            user.setEmail("123456@qq.com");
            user.setPhone("1111233232");
            return user;
        }
        return null;*/
        UserVo userVo = new UserVo();
        User user = userDao.findUserByUsernameAndByPassword(username,password);
        if(user!=null){
            BeanUtils.copyProperties(user,userVo);
            userVo.setToken("jkjkjkjkjkjkjkjkjk");
            return userVo;
        }

        return null;

    }

    /*@PostMapping("/api/login")
    public ApiResult<UserVo> apilogin(String username, String password){
        System.out.println("username="+username+",password="+password);
        *//*UserVo user =  new UserVo();
        if("admin".equals(username) && "123456".equals(password)){
            user.setId(1);
            user.setEmail("123456@qq.com");
            user.setPhone("1111233232");
            return user;
        }
        return null;*//*
        UserVo userVo = new UserVo();
        User user = userDao.findUserByUsernameAndByPassword(username,password);
        if(user!=null){
            BeanUtils.copyProperties(user,userVo);
            userVo.setToken("jkjkjkjkjkjkjkjkjk");
           *//* ApiResult apiResult = new ApiResult<UserVo>();
            apiResult.setCode(ApiConstant.API_SUCCUSS);
            apiResult.setMsg("操作成功");
            apiResult.setData(userVo);
            return apiResult;*//*
            return ApiResult.success(userVo);
        }
       *//* ApiResult apiResult = new ApiResult<UserVo>();
        apiResult.setCode(ApiConstant.ERR_LOGIN_FAIL);
        apiResult.setMsg("用户名或者密码错误");
        return apiResult;*//*
        return ApiResult.fail(ApiConstant.ERR_LOGIN_FAIL,"用户名或者密码错误");

    }*/

    @PostMapping("/api/login")
    public ApiResult<UserVo> apilogin(@RequestBody UserQuery userQuery){
        System.out.println("username="+userQuery.getUsername()+",password="+userQuery.getPassword());
        /*UserVo user =  new UserVo();
        if("admin".equals(username) && "123456".equals(password)){
            user.setId(1);
            user.setEmail("123456@qq.com");
            user.setPhone("1111233232");
            return user;
        }
        return null;*/
        UserVo userVo = new UserVo();
        User user = userDao.findUserByUsernameAndByPassword(userQuery.getUsername(),userQuery.getPassword());
        if(user!=null){
            BeanUtils.copyProperties(user,userVo);
            userVo.setToken("jkjkjkjkjkjkjkjkjk");
           /* ApiResult apiResult = new ApiResult<UserVo>();
            apiResult.setCode(ApiConstant.API_SUCCUSS);
            apiResult.setMsg("操作成功");
            apiResult.setData(userVo);
            return apiResult;*/
            return ApiResult.success(userVo);
        }
       /* ApiResult apiResult = new ApiResult<UserVo>();
        apiResult.setCode(ApiConstant.ERR_LOGIN_FAIL);
        apiResult.setMsg("用户名或者密码错误");
        return apiResult;*/
        return ApiResult.fail(ApiConstant.ERR_LOGIN_FAIL,"用户名或者密码错误");

    }

}
